<?php
class ZPoint {
	/**
		添加删除积分动作
		
	**/
	static public function Create($point, $user_id, $action='charge',$note,$adduser_id=0,$order_id=0){
		if (!$point|| !$user_id) return 0;
		//update user score;
		$user = Table::Fetch('user', $user_id);
		Table::UpdateCache('user', $user_id, array(
					'points' => array( "`points`+{$point}" ),
					));
		$u = array(
				'point' =>$point,
				'user_id' => $user_id,
				'action' => $action,
				'note' => $note,
				'adduser_id' => $adduser_id,
				'order_id' => $order_id,
				'addtime' => time(),
				);
		return DB::Insert('click_log', $u);
	}
	
	static public function PinTuanBuy($pin_id)
	{
		global $INI;
		$open_tuan_user_arr = get_pintuan_tuanzhang($pin_id);
		$tuanzhagn_info = $open_tuan_user_arr['tuanzhang'];
		$tuanyuan_arr = $open_tuan_user_arr['tuanyuan_arr'];
		//var_dump($INI['system']['tuanzbuypoints']);die();
		if(isset($INI['system']['tuanzbuypoints']) && $INI['system']['tuanzbuypoints'] >0)
		{
			$note = '团长开团成功，积分增加:'.$INI['system']['tuanzbuypoints'];
			$rs = self::Create($INI['system']['tuanzbuypoints'], $tuanzhagn_info['id'], 'pinbuy',$note,0,$tuanzhagn_info['order_id']);
			
		}
		foreach($tuanyuan_arr as $tuanyuan_info)
		{
			if(isset($INI['system']['tuanybuypoints']) && $INI['system']['tuanybuypoints'] >0)
			{
				$note = '团员参团成功，积分增加:'.$INI['system']['tuanybuypoints'];
				self::Create($INI['system']['tuanybuypoints'], $tuanyuan_info['id'], 'pinbuy',$note,0,$tuanyuan_info['order_id']);
			}
		}
		
	}
	
	static public function Invite($user_id,$adduser_id) {
		global $INI; 
		if ($INI['system']['clickpoints']==0) return ;
		$note = '团长邀请新增会员，积分增加:'.$INI['system']['clickpoints'];
		self::Create($INI['system']['clickpoints'], $user_id, 'invite',$note,$adduser_id);
		notify_addnew_click($user_id,$adduser_id);
	}

    static public function Refund($user_id=0, $order=array()) {
		global $INI; 
		if ($INI['credit']['buy']>0 ) {
			self::Create(-$INI['credit']['buy'], $user_id, 'refund', $order['team_id']);
		}
		$pay = abs(intval($INI['credit']['pay'] * $order['money']));
		if ($pay > 0) {
			self::Create(-$pay, $user_id, 'refund', $order['id']);
		}
	}
	static public function Charge($user_id=0, $money=0) {
		global $INI; if ($INI['credit']['charge']==0) return ;
		$pay = abs(intval($INI['credit']['charge'] * $money));
		if ($pay > 0) {
			self::Create($pay, $user_id, 'charge');
		}
	}

	static public function Explain($record) {
		if (!$record) return null;
		$action = $record['action'];
		if ('charge' == $action) {
			return  "网站充值";
		}
		else if ( 'buy' == $action) {
			$team = Table::Fetch('team', $record['detail_id']);
			return "购买商品 - {$team['product']}";
		}
		else if ( 'invite' == $action) {
			return '邀请好友';
		}
		else if ( 'register' == $action) {
			return '注册用户';
		}
		else if ( 'login' == $action) {
			return '每日登录';
		}
		else if ( 'exchange' == $action) {
			$goods = Table::Fetch('goods', $record['detail_id']);
			return "兑换商品 - {$goods['title']}";
		}
		else if ( 'pay' == $action ) {
			$order = Table::Fetch('order', $record['detail_id']);
			return "购买返积 - 消费金额：{$order['money']}";
		}
		else if ( 'charge' == $action) {
			return '在线充值';
		}
        else if ( 'refund' == $action) {
			return '项目退款';
		}
        else if ( 'comment' == $action) {
			return '消费评价';
		}
        else if ( 'daysign' == $action) {
			return '每日签到';
		}
	}
}
?>
